<template>


  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      :rules="dataRule"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="计划名称" prop="planName">
        <el-input v-model="dataForm.planName" placeholder="计划名称"></el-input>
      </el-form-item>
      <el-form-item label="机器人" prop="robotUrl">
        <el-input
          v-model="dataForm.robotUrl"
          placeholder="企业微信机器人地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="请求内容" prop="requestBody">
        <el-input
          v-model="dataForm.requestBody"
          placeholder="请求内容"
        ></el-input>
      </el-form-item>
      <el-form-item label="计划时间">
        <el-date-picker
          v-model="dataForm.time"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="执行人" prop="createUser">
        <el-input v-model="dataForm.createUser" placeholder="执行人"></el-input>
      </el-form-item>
      <el-form-item label="提醒人" prop="remindUser">
        <el-input v-model="dataForm.remindUser" placeholder="提醒人"></el-input>
      </el-form-item>
      <el-form-item label="压测节点" prop="stressServer">
        <el-select
          v-model="dataForm.stressServer"
          multiple
          placeholder="请选择"
        >
          <el-option
            v-for="item in dataForm.options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="服务器" prop="service">
        <el-input v-model="dataForm.service" placeholder="服务器"></el-input>
      </el-form-item>
      <el-form-item label="详情" prop="stressplan">
        <el-input
          v-model="dataForm.stressplan"
          placeholder="性能测试计划详情"
        ></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        robotUrl:
          "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e16fed90-2329-4ef6-bd9b-f35f31874272",
        requestBody: "",
        createUser: "",
        planName: "",
        remindUser: "all,",
        service: "",
        stressplan: "",
        stressServer: [],
        pageIndex: 1,
        pageSize: 10,
        time: [],
        options: [
          {
            value: "yace001",
            label: "yace001",
          },
          {
            value: "yace002",
            label: "yace002",
          },
          {
            value: "yace003",
            label: "yace003",
          },
          {
            value: "yace004",
            label: "yace004",
          },
          {
            value: "yace005",
            label: "yace005",
          },
        ],
      },
      dataRule: {
        planName: [
          {required: true, message: '测试计划不能为空', trigger: 'blur'}
        ],
        robotUrl: [
          {required: true, message: '地址不能为空', trigger: 'blur'}
        ],
        requestBody: [
          {required: true, message: '请求头不能为空', trigger: 'blur'}
        ],
        time: [
          {required: true, message: '测试时间不能为空', trigger: 'blur'}
        ],
        createUser: [
          {required: true, message: '执行人不能为空', trigger: 'blur'}
        ],
        remindUser: [
          {required: true, message: '提醒人不能为空', trigger: 'blur'}
        ],
        stressServer: [
          {required: true, message: '服务器不能为空', trigger: 'blur'}
        ],
        options: [
          {required: true, message: '压测节点不能为空', trigger: 'blur'}
        ],
        service: [
          {required: true, message: '服务不能为空', trigger: 'blur'}
        ],
        stressplan: [
          {required: true, message: '测试详情不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl(
          "/test/plan/list/" +
          this.dataForm.pageIndex +
          "/" +
          this.dataForm.pageSize
        ),
        method: "get",
      })
        .then(({data}) => {
          this.roleList = data && data.code === 0 ? data.list : [];
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/test/plan/list/${this.dataForm.id}`),
              method: "get",
            }).then(({data}) => {
              if (data && data.code === 0) {
                console.log(data.testPlan);
                this.dataForm.planName = data.testPlan.planName;
                this.dataForm.robotUrl = data.testPlan.robotUrl;
                this.dataForm.requestBody = data.testPlan.requestBody;
                this.dataForm.time[0] = data.testPlan.beginTime;
                this.dataForm.time[1] = data.testPlan.endTime;
                this.dataForm.createUser = data.testPlan.createUser;
                this.dataForm.remindUser = data.testPlan.remindUser;
                this.dataForm.service = data.testPlan.service;
                this.dataForm.stressplan = data.testPlan.stressplan;
                let c = data.testPlan.stressServer
                  .replaceAll('"', "")
                  .replaceAll("[", "")
                  .replaceAll("]", "")
                  .split(",");
                this.dataForm.stressServer = c;
                console.log(c);
                console.log(this.dataForm.time);
                console.log(this.dataForm);
              }
            });
          }
        });
    },
    // 表单提交
    dataFormSubmit() {
      console.log("新的时间选择器", this.dataForm.time);

      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/test/plan/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              robotUrl: this.dataForm.robotUrl,
              requestBody: this.dataForm.requestBody,
              createUser: this.dataForm.createUser,
              planName: this.dataForm.planName,
              stressServer: JSON.stringify(this.dataForm.stressServer),
              remindUser: this.dataForm.remindUser,
              service: this.dataForm.service,
              stressplan: this.dataForm.stressplan,
              endTime: this.dataForm.time[1],
              beginTime: this.dataForm.time[0],
            }),
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
